package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.CrossPeakStrongholdCacheData;
import com.yanqu.road.entity.activity.peakstronghold.CrossUnionAbilityCacheData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossPeakStrongholdCacheDaoImpl extends TempDataObjectDao<CrossPeakStrongholdCacheData> {
    public static String getCrossPeakStrongholdCacheDataSql = "select * from t_c_activity_peak_stronghold_cache where activityId=? and unionGroup=?";

    public CrossPeakStrongholdCacheData getCrossPeakStrongholdCacheData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CrossUnionAbilityCacheData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossPeakStrongholdCacheDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossPeakStrongholdCacheData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossPeakStrongholdCacheDataSql = "update t_c_activity_peak_stronghold_cache set round=?,stageType=?,waitServerIds=?,minOpenTime=?,waitError=? where activityId=? and unionGroup=?";

    @Override
    protected boolean updateData(CrossPeakStrongholdCacheData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWaitServerIds())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMinOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWaitError()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossPeakStrongholdCacheDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossPeakStrongholdCacheDataSql = "insert into t_c_activity_peak_stronghold_cache(activityId,unionGroup,round,stageType,waitServerIds,minOpenTime,waitError) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossPeakStrongholdCacheData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWaitServerIds())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMinOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWaitError()));
        if (getDb().executeNoneQuery(insertCrossPeakStrongholdCacheDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossPeakStrongholdCacheData parseData(ResultSet rs) throws SQLException {
        CrossPeakStrongholdCacheData data = new CrossPeakStrongholdCacheData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionGroup(rs.getInt("unionGroup"));
        data.setRound(rs.getInt("round"));
        data.setStageType(rs.getInt("stageType"));
        data.setWaitServerIds(JSON.parseObject(rs.getString("waitServerIds"), new TypeReference<List<Long>>() {
        }));
        data.setMinOpenTime(rs.getLong("minOpenTime"));
        data.setWaitError(rs.getInt("waitError"));

        return data;
    }
}
